Search


📜 [專欄新文章] Solidity Weekly #10
✍️ mingd...

  • Share this:


📜 [專欄新文章] Solidity Weekly #10
✍️ mingderwang
📥 歡迎投稿: https://medium.com/taipei-ethereum-meetup #徵技術分享文 #使用心得 #教學文 #medium

keccak256(abi.encodePacked(…))

最近如果你使用 solidity 0.4.24 以上版本編譯; 用到一些 hash functions 例如keccak256()、sha256() 或 ripemd160(),如果你帶入多個參數 (例如 keccak256(“a”, “b”, “c”)),你會看到如下的警示:

Warning: This function only accepts a single "bytes" argument. Please use "abi.encodePacked(...)" or a similar function to encode the data. keccak256( ^ (Relevant source part starts here and spans across multiple lines)

原因是,以前這些 functions 可以接受多個參數,連在ㄧ起後做一個 hash,但由於不同值在預設 type 被自動 padding 會有不同的結果,再加上多個參數串起來,容易出現不同的 hash 結果, 造成 bugs。所以決定 0.5.0 以後改只接受一個參數。因此,可以利用 abi.encodePacked() 先做一次包裝,再當成一個參數進行 hash。

補充說明,所謂的 padding 就是用 0 填滿到 bytes32 的格式,最典型的例子就是 call 一個function,裡面的參數(parameters) 就會被轉型成 bytes32 格式。

0x0000000000000000000000000000000000000000000000000000000000000045:

a uint32 value 69 padded to 32 bytes

0x0000000000000000000000000000000000000000000000000000000000000001:

boolean true, padded to 32 bytes

0x6162630000000000000000000000000000000000000000000000000000000000:

a bytes3 value "abc" 會被靠左對齊成 32 bytes

links 分享;

ERC20 API: An Attack Vector on Approve/TransferFrom Methods — (Mikhail Vladimirov, Dmitry Khovratovich)

ERC-1155: The Crypto Item Standard — (Witek Radomski)

*適用於 solidity ^0.4.24;

Solidity Weekly #10 was originally published in Taipei Ethereum Meetup on Medium, where people are continuing the conversation by highlighting and responding to this story.

👏 歡迎轉載分享鼓掌


Tags:

About author
not provided
We have regular meeting twice per month on discussing blockchain technology, smart contracts and DApps development! We would love to have you join us!
View all posts